package com.jxd.project.travel.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxd.project.travel.service.ITicketService;
import com.jxd.project.travel.dto.TicketQueryDTO;
import com.jxd.project.travel.model.Ticket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName TicketController
 * @Description TODO
 * @Author 邢子剑
 * @Date 2025/9/24 上午9:10
 * @Version 1.0
 */
@RestController
public class TicketController {
    @Autowired
    private ITicketService ticketService;

    @PostMapping("/ticketList")
    public IPage<Ticket> getTicketList(@RequestBody TicketQueryDTO queryDTO) {
        Integer page = queryDTO.getPage();
        Integer limit = queryDTO.getLimit();
        String departure = queryDTO.getDeparture();
        String arrival = queryDTO.getArrival();
        Integer empno = queryDTO.getEmpno();
        return ticketService.getTicketList(page, limit,  departure, arrival, empno);
    }

    @RequestMapping("/insertTicket")
    public boolean insertTicket(@RequestBody Ticket ticket) {
        return ticketService.insertTicket(ticket);
    }

    @RequestMapping("/updateTicket")
    public boolean updateTicket(@RequestBody Ticket ticket) {
        return ticketService.updateTicket(ticket);
    }

    @RequestMapping("/deleteTicket")
    public boolean deleteTicket(int ticketId) {
        return ticketService.deleteTicket(ticketId);
    }

    @RequestMapping("/deleteTicketBatch")
    public boolean deleteTicketBatch(@RequestBody int[] ticketIds) {
        List<Integer> tickets = new ArrayList<>();
        for (int i = 0; i < ticketIds.length; i++) {
            tickets.add(ticketIds[i]);
        }
        return ticketService.deleteTicketBatch(tickets);
    }


    //查询未绑定车票和已选车票
    @RequestMapping("/getTicketByEmpno")
    public List<Ticket> getTicketByEmpno(Integer empno, String travelId) {
        return ticketService.getTicketByEmpno(empno, travelId);
    }

    @RequestMapping("/deleteTravelIdInTicket")
    public boolean deleteTicketByTravelId(List<String> travelId) {
        return ticketService.deleteTravelIdInTicket(travelId);
    }

    //计算同一差旅中的票据总价
    @RequestMapping("/getAllTicketPrice")
    public double getAllTicketPrice(int[] ticketIds) {
        return ticketService.getAllTicketPrice(ticketIds);
    }

    // 重置旧车票的travelId为0
    @PostMapping("/resetTicketTravelId")
    public boolean resetTicketTravelId(@RequestParam String travelId) {
        return ticketService.resetTravelIdInTicket(travelId);
    }

    //绑定车票所对应的差旅信息
    @RequestMapping("/setTravelId")
    public List<Ticket> setTravelId(String travelId, int[] ticketIds) {
        return ticketService.setTravelId(travelId, ticketIds);
    }



    //通过车票List最早开始时间和最晚结束时间，计算出差旅时间
    @RequestMapping("/getTravelTime")
    public double getTravelTime(@RequestBody List<Ticket> tickets) {
        return ticketService.getTravelTime(tickets);
    }

    //修改票据时获取票据信息
    @GetMapping("/getTicketById")
    public Ticket getTicketById(@RequestParam String ticketId) {
        return ticketService.getOne(Integer.parseInt(ticketId));
    }

    //获取最新票据
    @RequestMapping("/getLastTicket")
    public Ticket getLastTicket(int empno) {
        return ticketService.getLastTicket(empno);
    }

}
